package com.yanqu.road.dao.impl.activity.share;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserQyfxData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/11/29 11:37
 */
public class QyfxActivityDataDaoImpl extends TempDataObjectDao<UserQyfxData> {
    public static String insertSql = "insert into t_u_qyfx_data(activityId, userId, sharePerson, dayResetTime) values(?,?,?,?)";
    @Override
    protected boolean addData(UserQyfxData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSharePerson()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDayResetTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_qyfx_data set sharePerson=?, dayResetTime=? where activityId = ? and userId = ?";
    @Override
    protected boolean updateData(UserQyfxData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getSharePerson()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDayResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserQyfxData parseData(ResultSet rs) throws SQLException {
        UserQyfxData userQyfxData = new UserQyfxData();
        userQyfxData.setActivityId(rs.getInt("activityId"));
        userQyfxData.setUserId(rs.getLong("userId"));
        userQyfxData.setSharePerson(rs.getInt("sharePerson"));
        userQyfxData.setDayResetTime(rs.getLong("dayResetTime"));
        return userQyfxData;
    }

    private static String getSql = "SELECT * FROM t_u_qyfx_data where activityId = ? and userId = ?";

    public UserQyfxData getQyfxActivityData(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserQyfxData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }
}
